<template>
  <div class="content-wrapper">
    <div class="nav">
      <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/administration-rightsSetting' }">HR招聘</el-breadcrumb-item>
        <el-breadcrumb-item>招聘设置</el-breadcrumb-item>
      </el-breadcrumb>
      <span>
        <el-button type="primary" @click="showDeptDialog(false)" v-if="isAuth('recruitment:hrrecruitmentdept:save')">新增部门</el-button>
        <!-- <el-button type="primary" @click="syncDingDepartment" v-if="isAuth('recruitment:hrrecruitmentdept:save')">同步钉钉部门</el-button> -->
      </span>
    </div>
    <div class="content">
      <div class="dept-item" v-for="data in jobList" :key="data.id">
        <span class="click-icon" @click="data.show = !data.show">
          <i :class="{'el-icon-arrow-down' : data.jobVO && data.jobVO.length > 0 && data.show}"></i>
          <i :class="{'el-icon-arrow-right' : data.jobVO && data.jobVO.length > 0 && !data.show}"></i>
          <span v-if="data.jobVO && data.jobVO.length === 0" style="margin-left: 20px;"></span>
          {{ data.deptName }}
        </span>
        <span class="icon-style">
          <el-tooltip class="item" effect="dark" content="新增岗位" placement="bottom">
            <i class="el-icon-circle-plus-outline" @click.stop="showPositionDialog(false, data)" v-if="isAuth('recruitment:hrrecruitmentjob:save')"></i>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="面试考评设置" placement="bottom">
            <i class="el-icon-setting" @click.stop="showQuestionDialog(data)" v-if="isAuth('recruitment:hrrecruitmentexamination:list') && isAuth('recruitment:hrrecruitmentexamination:info')"></i>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑部门" placement="bottom">
            <i class="el-icon-edit-outline" @click.stop="showDeptDialog(data)" v-if="isAuth('recruitment:hrrecruitmentdept:save') || isAuth('recruitment:hrrecruitmentdept:update') || isAuth('recruitment:hrrecruitmentdept:delete')"></i>
          </el-tooltip>
        </span>
        <div v-if="data.show">
          <div v-for="child in data.jobVO" :key="child.id" class="row-2">
            <div class="job-name">{{child.jobName}}</div>
            <div>
              <span v-if="child.jobStatus==='1'">（{{child.recruitNum}}/{{child.jobNum}}）</span>
              <span v-if="child.jobStatus==='2'" style="color: #1989FA;">（招满）</span>
              <span v-if="child.jobStatus==='9'" style="color: #AEAEB0;">（取消）</span>
            </div>
            <div class="icon-style">
              <el-tooltip class="item" effect="dark" content="编辑岗位" placement="bottom">
                <i class="el-icon-edit-outline" @click="showPositionDialog(child.id, data)" v-if="isAuth('recruitment:hrrecruitmentjob:save') || isAuth('recruitment:hrrecruitmentjob:update') || isAuth('recruitment:hrrecruitmentjob:delete')"></i>
              </el-tooltip>
            </div>
          </div>
        </div>
      </div>
    </div>
    <AddOrEditDepartment ref="AddOrEditDepartment" @refresh="reload"></AddOrEditDepartment>
    <QuestionSetting ref="QuestionSetting" @refresh="getExaminationList"></QuestionSetting>
    <AddOrEditPosition ref="AddOrEditPosition" @refresh="reload"></AddOrEditPosition>
  </div>
</template>

<script>
import formUtil from '@/utils/formUtil'
import AddOrEditDepartment from './add-or-edit-department'
import QuestionSetting from './question-setting'
import AddOrEditPosition from './add-or-edit-position'
export default {
  data () {
    return {
      jobList: [],
      examinationList: [],
    };
  },
  components: {
    AddOrEditDepartment, QuestionSetting,  AddOrEditPosition
  },
  mounted () {
    this.getJobList(true);
  },
  methods: {
    async getJobList(init) {
      let list = await this.$httpN.postB('recruitment/hrrecruitmentjob/list');
      if(list) {
        list.forEach(element => { element.show = true});
        this.jobList = this.getFirstDept(list);
        if(init && this.isAuth('recruitment:hrrecruitmentexamination:list') && this.isAuth('recruitment:hrrecruitmentexamination:info')) this.getExaminationList();
      }
    },
    //获取一级部门列表
    getFirstDept(list) {
      let ids = list.filter( item => { return item.deptPid === '' }).map( item => {return item.id} )
      return list.filter( item => { return ids.includes(item.deptPid) })
    },
    showDeptDialog(data) {
      this.$nextTick(() => {
        this.$refs.AddOrEditDepartment.init(data);
      })
    },
    async getExaminationList() {
      let {list} = await this.$httpN.postB('recruitment/hrrecruitmentexamination/list', {limit: 999})
      this.examinationList = list
    },
    async showQuestionDialog(dept) {
      // let list = this.examinationList.filter( item => { return item.examinationDeptId === dept.id })
      // if(list.length === 0) {
      //   if(!await this.$httpN.postB('recruitment/hrrecruitmentexamination/save', { examinationDeptId: dept.id, examinationJobId: '', examinationName: dept.deptName+'面试卷'})) return;
      //   this.getExaminationList();
      //   this.$nextTick(() => {
      //     this.$refs.QuestionSetting.init(dept);
      //   })
      // } else {
        this.$nextTick(() => {
          this.$refs.QuestionSetting.init(dept);
        })
      // }
    },
    async showPositionDialog(id, dept) {
      this.$nextTick(() => {
        this.$refs.AddOrEditPosition.init(id, dept);
      })
    },
    reload() {
      this.getJobList(false);
    },
    async syncDingDepartment() {
      if(!await this.$httpN.postB('recruitment/hrrecruitmentdept/synDepartment')) return
      formUtil.submitSuccess('同步成功');
      this.getJobList(true);
    }
  }
}
</script>

<style lang='scss' scoped>
.content-wrapper {
  height: 100vh;
  min-width: 500px;
  .nav {
    display: flex;
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    justify-content: space-between;
    border-bottom: 1px solid #DCDFE6;
    .el-breadcrumb {
      display: flex;
      align-items: center;
      .el-breadcrumb__item {
        float: none;
      }
    }
  }
  .content {
    height: calc(100vh - 50px);
    overflow: auto;
    padding: 20px;
    color: #6E6E71;
    .dept-item {
      line-height: 30px;
      font-size: 15px;
      .click-icon {
        cursor: pointer;
        margin-right: 6px;
        font-weight: 600;
        color: #6E6E71;
        i {
          font-weight: 600;
          color: #6E6E71;
        }
      }
      .icon-style {
        font-size: 15px;
        i {
          color: #1989FA;
          cursor: pointer;
        }
      }
      .row-2 {
        padding-left: 40px;
        font-size: 13px;
        font-weight: 400;
        color: #6E6E71;
        display: flex;
        .job-name {
          margin-right: 6px;
        }
      }
    }
  }
}
.el-tree {
  /deep/ .el-tree-node__content {
    height: auto;
    align-items: start;
  }
}
.dialog-style /deep/ .el-dialog__body {
  padding: 0;
  .el-table td, .el-table th {
    padding: 6px 0;
  }
}
</style>